import router from './index';
import { Local } from '@/utils/storage';
import { LOGIN } from '@/common/static';
import { initDynamicRouter } from './modules/initDynamicRouter';
import menuStore from '@/stores/menu';
import { start, close } from '@/utils/nprogress';
/**
 * 全局路由守卫
 * @param {Object} to - 即将跳转的路由对象
 * @param {Object} from - 当前导航正要离开的路由对象
 * @param {Function} next - 用于进行路由跳转的函数
 */
router.beforeEach(async (to, from, next) => {
	start();
	// 判断是否前往登录页
	if (to.path == LOGIN) return next();
	// 判断有没有登录
	if (!Local.get('token')) return next({ path: LOGIN, replace: true });
	// 请求获取路由菜单  初始化动态路由
	const menu = menuStore();
	if (!menu.menuList.length) {
		await initDynamicRouter();
		return next({ ...to, replace: true });
	}
	next();
});
/**
 * 全局后置钩子
 * @param {Object} to - 即将跳转的路由对象
 * @param {Object} from - 当前导航正要离开的路由对象
 */
router.afterEach((to, from) => {
	close();
	// console.log(to);
	// console.log(from);
});
